<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libfuse: example/ioctl.c File Reference</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">libfuse
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_cfafba98a580ce4b62f8a6fa96d7cbb0.html">example</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">ioctl.c File Reference</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><code>#include &lt;<a class="el" href="fuse_8h_source.html">fuse.h</a>&gt;</code><br />
<code>#include &lt;stdlib.h&gt;</code><br />
<code>#include &lt;stdio.h&gt;</code><br />
<code>#include &lt;string.h&gt;</code><br />
<code>#include &lt;unistd.h&gt;</code><br />
<code>#include &lt;time.h&gt;</code><br />
<code>#include &lt;errno.h&gt;</code><br />
<code>#include &quot;<a class="el" href="ioctl_8h_source.html">ioctl.h</a>&quot;</code><br />
</div>
<p><a href="ioctl_8c_source.html">Go to the source code of this file.</a></p>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This example illustrates how to write a FUSE file system that can process (a restricted set of) ioctls. It can be tested with the <a class="el" href="ioctl__client_8c.html">ioctl_client.c</a> program.</p>
<p>Compile with: </p><pre class="fragment">gcc -Wall ioctl.c `pkg-config fuse3 --cflags --libs` -o ioctl
</pre><h2>Source code</h2>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">  FUSE fioc: FUSE ioctl example</span></div><div class="line"><span class="comment">  Copyright (C) 2008       SUSE Linux Products GmbH</span></div><div class="line"><span class="comment">  Copyright (C) 2008       Tejun Heo &lt;teheo@suse.de&gt;</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">  This program can be distributed under the terms of the GNU GPL.</span></div><div class="line"><span class="comment">  See the file COPYING.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define FUSE_USE_VERSION 31</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="fuse_8h.html">fuse.h</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;errno.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="ioctl_8h.html">ioctl.h</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define FIOC_NAME       &quot;fioc&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">enum</span> {</div><div class="line">        FIOC_NONE,</div><div class="line">        FIOC_ROOT,</div><div class="line">        FIOC_FILE,</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> *fioc_buf;</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">size_t</span> fioc_size;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_resize(<span class="keywordtype">size_t</span> new_size)</div><div class="line">{</div><div class="line">        <span class="keywordtype">void</span> *new_buf;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (new_size == fioc_size)</div><div class="line">                <span class="keywordflow">return</span> 0;</div><div class="line"></div><div class="line">        new_buf = realloc(fioc_buf, new_size);</div><div class="line">        <span class="keywordflow">if</span> (!new_buf &amp;&amp; new_size)</div><div class="line">                <span class="keywordflow">return</span> -ENOMEM;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (new_size &gt; fioc_size)</div><div class="line">                memset(new_buf + fioc_size, 0, new_size - fioc_size);</div><div class="line"></div><div class="line">        fioc_buf = new_buf;</div><div class="line">        fioc_size = new_size;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_expand(<span class="keywordtype">size_t</span> new_size)</div><div class="line">{</div><div class="line">        <span class="keywordflow">if</span> (new_size &gt; fioc_size)</div><div class="line">                <span class="keywordflow">return</span> fioc_resize(new_size);</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_file_type(<span class="keyword">const</span> <span class="keywordtype">char</span> *path)</div><div class="line">{</div><div class="line">        <span class="keywordflow">if</span> (strcmp(path, <span class="stringliteral">&quot;/&quot;</span>) == 0)</div><div class="line">                <span class="keywordflow">return</span> FIOC_ROOT;</div><div class="line">        <span class="keywordflow">if</span> (strcmp(path, <span class="stringliteral">&quot;/&quot;</span> FIOC_NAME) == 0)</div><div class="line">                <span class="keywordflow">return</span> FIOC_FILE;</div><div class="line">        <span class="keywordflow">return</span> FIOC_NONE;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_getattr(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> stat *stbuf,</div><div class="line">                        <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line">        stbuf-&gt;st_uid = getuid();</div><div class="line">        stbuf-&gt;st_gid = getgid();</div><div class="line">        stbuf-&gt;st_atime = stbuf-&gt;st_mtime = time(NULL);</div><div class="line"></div><div class="line">        <span class="keywordflow">switch</span> (fioc_file_type(path)) {</div><div class="line">        <span class="keywordflow">case</span> FIOC_ROOT:</div><div class="line">                stbuf-&gt;st_mode = S_IFDIR | 0755;</div><div class="line">                stbuf-&gt;st_nlink = 2;</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">        <span class="keywordflow">case</span> FIOC_FILE:</div><div class="line">                stbuf-&gt;st_mode = S_IFREG | 0644;</div><div class="line">                stbuf-&gt;st_nlink = 1;</div><div class="line">                stbuf-&gt;st_size = fioc_size;</div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">        <span class="keywordflow">case</span> FIOC_NONE:</div><div class="line">                <span class="keywordflow">return</span> -ENOENT;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_open(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (fioc_file_type(path) != FIOC_NONE)</div><div class="line">                <span class="keywordflow">return</span> 0;</div><div class="line">        <span class="keywordflow">return</span> -ENOENT;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_do_read(<span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> size, off_t offset)</div><div class="line">{</div><div class="line">        <span class="keywordflow">if</span> (offset &gt;= fioc_size)</div><div class="line">                <span class="keywordflow">return</span> 0;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (size &gt; fioc_size - offset)</div><div class="line">                size = fioc_size - offset;</div><div class="line"></div><div class="line">        memcpy(buf, fioc_buf + offset, size);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> size;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_read(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> size,</div><div class="line">                     off_t offset, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (fioc_file_type(path) != FIOC_FILE)</div><div class="line">                <span class="keywordflow">return</span> -EINVAL;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> fioc_do_read(buf, size, offset);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_do_write(<span class="keyword">const</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> size, off_t offset)</div><div class="line">{</div><div class="line">        <span class="keywordflow">if</span> (fioc_expand(offset + size))</div><div class="line">                <span class="keywordflow">return</span> -ENOMEM;</div><div class="line"></div><div class="line">        memcpy(fioc_buf + offset, buf, size);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> size;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_write(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keyword">const</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">size_t</span> size,</div><div class="line">                      off_t offset, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (fioc_file_type(path) != FIOC_FILE)</div><div class="line">                <span class="keywordflow">return</span> -EINVAL;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> fioc_do_write(buf, size, offset);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_truncate(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, off_t size,</div><div class="line">                         <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line">        <span class="keywordflow">if</span> (fioc_file_type(path) != FIOC_FILE)</div><div class="line">                <span class="keywordflow">return</span> -EINVAL;</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> fioc_resize(size);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_readdir(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">void</span> *buf, <a class="code" href="fuse_8h.html#a7dd132de66a5cc2add2a4eff5d435660">fuse_fill_dir_t</a> filler,</div><div class="line">                        off_t offset, <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi,</div><div class="line">                        <span class="keyword">enum</span> <a class="code" href="fuse_8h.html#af2bcf2a473b41b3cc8da8c079656a074">fuse_readdir_flags</a> flags)</div><div class="line">{</div><div class="line">        (void) fi;</div><div class="line">        (void) offset;</div><div class="line">        (void) flags;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (fioc_file_type(path) != FIOC_ROOT)</div><div class="line">                <span class="keywordflow">return</span> -ENOENT;</div><div class="line"></div><div class="line">        filler(buf, <span class="stringliteral">&quot;.&quot;</span>, NULL, 0, 0);</div><div class="line">        filler(buf, <span class="stringliteral">&quot;..&quot;</span>, NULL, 0, 0);</div><div class="line">        filler(buf, FIOC_NAME, NULL, 0, 0);</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> fioc_ioctl(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, <span class="keywordtype">int</span> cmd, <span class="keywordtype">void</span> *arg,</div><div class="line">                      <span class="keyword">struct</span> <a class="code" href="structfuse__file__info.html">fuse_file_info</a> *fi, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> flags, <span class="keywordtype">void</span> *data)</div><div class="line">{</div><div class="line">        (void) arg;</div><div class="line">        (void) fi;</div><div class="line">        (void) flags;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (fioc_file_type(path) != FIOC_FILE)</div><div class="line">                <span class="keywordflow">return</span> -EINVAL;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (flags &amp; FUSE_IOCTL_COMPAT)</div><div class="line">                <span class="keywordflow">return</span> -ENOSYS;</div><div class="line"></div><div class="line">        <span class="keywordflow">switch</span> (cmd) {</div><div class="line">        <span class="keywordflow">case</span> FIOC_GET_SIZE:</div><div class="line">                *(<span class="keywordtype">size_t</span> *)data = fioc_size;</div><div class="line">                <span class="keywordflow">return</span> 0;</div><div class="line"></div><div class="line">        <span class="keywordflow">case</span> FIOC_SET_SIZE:</div><div class="line">                fioc_resize(*(<span class="keywordtype">size_t</span> *)data);</div><div class="line">                <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> -EINVAL;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keyword">struct </span><a class="code" href="structfuse__operations.html">fuse_operations</a> fioc_oper = {</div><div class="line">        .<a class="code" href="structfuse__operations.html#ac39a0b7125a0e5001eb5ff42e05faa5d">getattr</a>        = fioc_getattr,</div><div class="line">        .readdir        = fioc_readdir,</div><div class="line">        .truncate       = fioc_truncate,</div><div class="line">        .open           = fioc_open,</div><div class="line">        .read           = fioc_read,</div><div class="line">        .write          = fioc_write,</div><div class="line">        .ioctl          = fioc_ioctl,</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">        <span class="keywordflow">return</span> <a class="code" href="fuse_8h.html#ac99b844cee7aaa8fb4e35df5b5488d82">fuse_main</a>(argc, argv, &amp;fioc_oper, NULL);</div><div class="line">}</div></div><!-- fragment --> 
<p class="definition">Definition in file <a class="el" href="ioctl_8c_source.html">ioctl.c</a>.</p>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>
